package com.scut.dispatching_server.tool;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import com.scut.dispatching_server.config.GlobalVariables;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

/**
 * ResponseTimePredictor
 *
 * @author yuxi
 * @date 2019-03-04
 */
public class ResponseTimePredictor {
    public String request(String driverId, String orderIds, String serverAddress) throws Exception{
        URL url = new URL(serverAddress);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        PrintWriter printWriter = new PrintWriter(conn.getOutputStream());
        printWriter.print("driverId=" + driverId + "&orderIds=" + orderIds);
        printWriter.flush();
        InputStream inputStream = conn.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
        StringBuilder result = new StringBuilder();
        String strRead = null;
        while((strRead = reader.readLine()) != null){
            result.append(strRead);
            result.append("\r\n");
        }
        reader.close();
        inputStream.close();
        conn.disconnect();
        return result.toString();
    }
    public ArrayList<float[]> getProba(ArrayList<String> orderIds, String driverId, int driverIndex) throws Exception{
        StringBuilder orderIdStrings = new StringBuilder();
        for(int i = 0; i < orderIds.size(); i++){
            orderIdStrings.append(orderIds.get(i));
            orderIdStrings.append(",");
        }
        StringBuilder serverAddressString = new StringBuilder();
        serverAddressString.append(GlobalVariables.SERVER_ADDRESS);
        serverAddressString.append(":");
        serverAddressString.append(GlobalVariables.SERVER_START_PORT + (driverIndex % GlobalVariables.SERVER_NUMBER));
        serverAddressString.append("/predict");
        String jsonString = request(driverId, orderIdStrings.deleteCharAt(orderIdStrings.lastIndexOf(",")).toString(), serverAddressString.toString());
        //不知道这个对象是不是线程安全的
        JsonParser parse = new JsonParser();
        JsonObject r = (JsonObject)parse.parse(jsonString);
        ArrayList<float[]> probResults = new ArrayList<>();
        for(int i = 0; i < orderIds.size(); i++){
            float[] proba = new float[GlobalVariables.RESPONSE_TIME_WINDOWS.length];
            String resultString = r.get(orderIds.get(i)).toString();
            resultString = resultString.substring(1, resultString.length() - 1);
            String[] resultArray = resultString.split(",");
            for(int j = 0; j < GlobalVariables.RESPONSE_TIME_WINDOWS.length; j++){
                proba[j] = Float.parseFloat(resultArray[j]);
            }
            probResults.add(proba);
        }
        return probResults;
    }
}